package com.wc.wcWebsite_core;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.wc.wcWebsite_api.UserService;
import com.wc.wcWebsite_mapper.UserMapper;
import com.wc.wcWebsite_model.CommonUtil;
import com.wc.wcWebsite_model.User;



@Service("userService")
public class UserServiceImpl implements UserService {
	@Autowired
	private UserMapper userMapper;
	
	@Override
	public Boolean addUser(User user) {
		int ret=userMapper.insert(user);
		if(ret>0){
			return true;
		}else{
			return false;
		}
		
	}

	@Override
	public User findUserByEmail(String email) {
		User user=userMapper.selectByEmail(email);
		return user;
	}

	@Override
	public String getName(){
		User user=new User();
		user.setUsername("wc");
		return user.getUsername();
	}

	@Override
	public Map<String, Object> userLogin(String email, String password) {
		Map<String,Object> retMap=new HashMap<String, Object>();
		Map<String,String> params=new HashMap<String, String>();
		params.put("email", email);
		params.put("password",CommonUtil.EncodeByMD5(password));
		User user=userMapper.selectByEmailAndPwd(params);
		if(user!=null){
			retMap.put("retCode", "0000");
			retMap.put("retObject", user);
		}else{
			retMap.put("retCode", "0001");
		}
		return retMap;
	}

	@Override
	@Cacheable(value="user")
	public User findUserByName(String name) {
		System.out.println("service findUserByName()--------从数据库获取"+name);
		User user=userMapper.selectByName(name);
		return user;
	}

	@Override
	public Boolean updateUser(User user) {
		// TODO Auto-generated method stub
		int ret=userMapper.updateById(user);
		if(ret>0){
			return true;
		}else{
			return false;
		}
	}

}
